﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;

using DTO;
using BLL;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.LookAndFeel;

namespace CRM.GUI.QLKhachHang
{
    public partial class frmView : DevExpress.XtraEditors.XtraForm
    {
        public frmView()
        {
            InitializeComponent();
            grdViewCoBan.BestFitColumns();
            grdViewCoBan.OptionsView.ColumnAutoWidth = false;
            grdViewCoBan.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            grdViewCoBan.HorzScrollVisibility = ScrollVisibility.Always;
            IntialLoad();
        }

        DataTranferEntity trans = new DataTranferEntity();
        private void IntialLoad()
        {
            cmbCity.View.Columns.AddField("RegionName").Visible = true;
            cmbCity.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 2).ToList();
            cmbCity.DisplayMember = "RegionName";
            cmbCity.ValueMember = "Region_Code";
            cmbDistrict.View.Columns.AddField("RegionName").Visible = true;
            cmbDistrict.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 3).ToList();
            cmbDistrict.DisplayMember = "RegionName";
            cmbDistrict.ValueMember = "Region_Code";
            cmbWard.View.Columns.AddField("RegionName").Visible = true;
            cmbWard.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 4).ToList();
            cmbWard.DisplayMember = "RegionName";
            cmbWard.ValueMember = "Region_Code";
            ExcelFunc func = new ExcelFunc();
            cmbErrorType.DisplayMember = "Key";
            cmbErrorType.ValueMember = "Value";
            cmbErrorType.DataSource = new BindingSource(func.ErrorList, null);

            grdColCount.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
            grdViewCoBan.CustomUnboundColumnData += grdViewCoBan_CustomUnboundColumnData;
        }
        
        private static DataTable _dt = new DataTable();
        private void frmView_FormClosed(object sender, FormClosedEventArgs e)
        {
            ExcelFunc func = new ExcelFunc();
            frmImport f = new frmImport();

            MessageBox.Show("Vì file này vẫn còn lỗi nên hệ thống sẽ không nhận!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
            func.ErrorList.Clear();
            f.Close();
        }

        private void cmbErrorType_SelectedValueChanged(object sender, EventArgs e)
        {
            ExcelFunc func = new ExcelFunc();
            if (cmbErrorType.SelectedValue != null)
            {
                var value = cmbErrorType.SelectedValue;
                grdErrorView.DataSource = value;
                lblCountError.Text = (value as DataTable).Rows.Count.ToString();
            }
        }

        private void grdViewCoBan_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value = e.ListSourceRowIndex + 1;
        }

        private void btnExportToExcel_Click(object sender, EventArgs e)
        {
            grdViewCoBan.OptionsPrint.AutoWidth = false;
            grdViewCoBan.BestFitColumns();
            SaveFileDialog SaveDlg = new SaveFileDialog();
            SaveDlg.Filter = "*.xls|*.xlsx";
            SaveDlg.FileName = "Report.xlsx";
            if (SaveDlg.ShowDialog() != DialogResult.Cancel)
            {
                grdViewCoBan.BestFitColumns();

                DevExpress.XtraPrinting.XlsxExportOptions opt = new DevExpress.XtraPrinting.XlsxExportOptions()
                {
                    TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text,
                    ExportMode = DevExpress.XtraPrinting.XlsxExportMode.SingleFile,
                    ShowGridLines = true,
                };
                grdViewCoBan.ExportToXlsx(SaveDlg.FileName.ToString(), opt);                
            }
        }
        
    }
}